package cn.yi.medicalsys.controller;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import cn.yi.medicalsys.pojo.Doctor;
import cn.yi.medicalsys.pojo.Office;
import cn.yi.medicalsys.pojo.PageBean;
import cn.yi.medicalsys.service.DoctorService;
import cn.yi.medicalsys.service.OfficeService;
import cn.yi.medicalsys.service.impl.DoctorServiceImpl;
import cn.yi.medicalsys.service.impl.OfficeServiceImpl;

public class SearchDoctorServlet extends HttpServlet {

	private static final long serialVersionUID = 1L;

	public void destroy() {
		super.destroy(); // Just puts "destroy" string in log
	}

	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		this.doPost(request, response);
	}

	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		PrintWriter out = response.getWriter();
		response.setContentType("text/html;charset=utf-8");
		HttpSession session = request.getSession();
		request.setCharacterEncoding("utf-8");
		String queryName = request.getParameter("depname");
		String queryOffice = request.getParameter("office");
		int currentPageNo = 1; // 默认页码
		int pageSize = 5;
		if(queryOffice !=null && queryOffice.equals("0")){
			queryOffice=null;
		}
		DoctorService doctor = new DoctorServiceImpl();
		// 总页数
		int totalPageCount = doctor.findTotalPage(pageSize, queryName,
				queryOffice);
		if(totalPageCount>0){					
		System.out.println("空 总页数"+totalPageCount+"科室"+queryOffice+"人名"+queryName);
		String pageNo = request.getParameter("pageNo");
		if (pageNo != null && !"".equals(pageNo)) {
			currentPageNo = Integer.valueOf(pageNo);
		}
		// 控制获取的页码
		if (currentPageNo == 0) {
			currentPageNo = 1;
		}
		if (currentPageNo >= totalPageCount) {
			currentPageNo = totalPageCount;
		}
		PageBean<Doctor> pageBean = doctor.findDoctorByPage(currentPageNo,
				pageSize, queryName, queryOffice);
		pageBean.setCurrPageNo(currentPageNo);
		List<Doctor> list = pageBean.getList();
		OfficeService office = new OfficeServiceImpl();
		List<String> listOff = new ArrayList<String>();
		for (int i = 0; i < list.size(); i++) {
			listOff.add(office.findOfficeByNo(list.get(i).getoId()).getOfficeName());
		}
		List<Office> listoffice = office.findAllOffice();
		session.setAttribute("listoffice", listoffice);
		
		session.setAttribute("listOff", listOff);
		session.setAttribute("bean", pageBean);// 分页对象
		session.setAttribute("depname", queryName);// 设置session 对象
		session.setAttribute("office", queryOffice);//
		out.print(11);
		out.print(pageBean.getTotalPageCount());
		response.sendRedirect("staff.jsp");
		}else{
			out.print("<script>alert('未查询到此医生');history.back()</script>");
		}
	}

	public void init() throws ServletException {
	}

}
